#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,T,K,ans,a[100005],b[100005],c[100005],g[20][100005],l[10];
vector<ll> p,q;
ll dg(int dep){
    if(p.size()==1) return p[0];
    q.clear();
    for(int i=0;i<p.size();i+=2){
        if(g[dep][i/2+1]==0){
            if(b[p[i]]>=dep) q.push_back(p[i]);
            else q.push_back(p[i+1]);
        }
        else{
            if(b[p[i+1]]>=dep) q.push_back(p[i+1]);
            else q.push_back(p[i]);
        }
    }
    p.clear();
    for(int i=0;i<q.size();i++) p.push_back(q[i]);
    return dg(dep+1);
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=m;i++) scanf("%lld",&c[i]);
    while((1<<K)<n) K++;
    for(int i=1;i<=K;i++){
        for(int j=1;j<=(1<<(K-i));j++){
            char cc;
            cin>>cc;
            if(cc=='1') g[i][j]=1;
        }
    }
    cin>>T;
    while(T--){
        ans=0;
        scanf("%lld%lld%lld%lld",&l[0],&l[1],&l[2],&l[3]);
        for(int i=1;i<=n;i++){
            b[i]=a[i]^l[i%4];
        }
        for(int i=1;i<=m;i++){
            p.clear();
            for(int j=1;j<=c[i];j++) p.push_back(j);
            ans^=i*dg(1);
        }
        cout<<ans<<endl;
    }
    return 0;
}
